package com.iemes.controller.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.ChildFormMap;
import com.iemes.entity.ClientinfoMap;
import com.iemes.entity.ProductFormMap;
import com.iemes.entity.Stocktomat;
import com.iemes.mapper.UserMapper;
import com.iemes.util.Common;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/product/")
public class ProductController extends BaseController {
    @Inject
    private UserMapper userMapper;
    //产品管理
    @RequestMapping("product")
    public String productManagement(Model model){
        String [] btnArray = new String[]{"<button type=\"button\" id=\"addAccount\" class=\"btn btn-primary marR10\">新增</button>","<button type=\"button\" id=\"editAccount\" class=\"btn btn-info marR10\">编辑</button>","<button type=\"button\" id=\"delAccount\" class=\"btn btn-danger marR10\">删除</button>"};
        model.addAttribute("res", findByRes());
        model.addAttribute("btn",btnArray);
        return Common.BACKGROUND_PATH + "/system/user/productManagement";
    }
    @ResponseBody
    @RequestMapping("productManagementPage")
    public PageInfo productManagement(String pageNow,
                                      String pageSize, String column, String sort){
        PageHelper.startPage(1, 10);
        List<ProductFormMap> produceMappers = userMapper.findProductPage();
        PageInfo pageInfo = new PageInfo<>(produceMappers);
        return pageInfo;

    }

    @ResponseBody
    @RequestMapping("addProduct")
//    @SystemLog(module="系统管理",methods="客户管理-新增客户")//凡需要处理业务逻辑的.都需要记录操作日志
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    public String addProductManagement(){
        //获取数据
        ProductFormMap productFormMap = getFormMap(ProductFormMap.class);
        try {
            userMapper.addEntity(productFormMap);//新增后返回新增信息
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }

    @ResponseBody
    @RequestMapping("editProduct")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
//    @SystemLog(module="系统管理",methods="用户管理-修改用户")//凡需要处理业务逻辑的.都需要记录操作日志
    public String editEntity(String txtGroupsSelect) throws Exception {
        ProductFormMap productFormMap = getFormMap(ProductFormMap.class);
        productFormMap.put("txtGroupsSelect", txtGroupsSelect);
        userMapper.deleteByAttribute("itemId", productFormMap.get("itemId")+"", ProductFormMap.class);
        userMapper.addEntity(productFormMap);
        return "success";
    }
    //删除
    @ResponseBody
    @RequestMapping("deleteProduct")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    @SystemLog(module="系统管理",methods="用户管理-删除用户")//凡需要处理业务逻辑的.都需要记录操作日志
    public String delProduct() throws Exception {
        String[] ids = getParaValues("ids");
        for (String id : ids) {
            userMapper.deleteByAttribute("itemId", id, ProductFormMap.class);
        }
        return "success";
    }

    //打开新增客户页面
    @RequestMapping("addProductManagementUI")
    public String addProductManagementUI(){
        return Common.BACKGROUND_PATH + "/system/product/addProduct";
    }

    @RequestMapping("editProductManagementUI")
    public String editProductManagementUI(Model model, HttpServletRequest request){
        String id = getPara("id");
        if(Common.isNotEmpty(id)){
            model.addAttribute("user", userMapper.findbyFrist("itemId", id, ProductFormMap.class));

        }
        return Common.BACKGROUND_PATH + "/system/product/editProduct";
    }

    //查询关联数据
    @ResponseBody
    @RequestMapping("getChild")
    public PageInfo getChild(HttpServletRequest request){
        PageHelper.startPage(1, 10);
        List<ChildFormMap> childFormMaps = userMapper.findChildPage(Integer.parseInt(request.getParameter("id")));
        PageInfo pageInfo = new PageInfo<>(childFormMaps);
        return pageInfo;
    }

    @ResponseBody
    @RequestMapping("addChild")
    public String addChild(){
        Stocktomat formMap = getFormMap(Stocktomat.class);
        try {
            userMapper.addEntity(formMap);//新增后返回新增信息
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }

    @ResponseBody
    @RequestMapping("editChild")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
//    @SystemLog(module="系统管理",methods="用户管理-修改用户")//凡需要处理业务逻辑的.都需要记录操作日志
    public String editChild(){
        Stocktomat stocktomat = getFormMap(Stocktomat.class);
        int stockid = Integer.parseInt(stocktomat.get("stockid").toString());
        int matid = Integer.parseInt(stocktomat.get("matid").toString());
        int stmnum = Integer.parseInt(stocktomat.get("stmnum").toString());
        userMapper.updateChild(stockid,matid,stmnum);
        return "success";
    }

    @ResponseBody
    @RequestMapping("deleteChild")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    @SystemLog(module="系统管理",methods="用户管理-删除用户")//凡需要处理业务逻辑的.都需要记录操作日志
    public String delChlld(HttpServletRequest request) throws Exception {
        String stockid = request.getParameter("stocktomat.stockid");
        String matid = request.getParameter("stocktomat.matid");
        String[] split = matid.split(",");
        for (String ma : split){
            userMapper.delChild(Integer.parseInt(stockid),Integer.parseInt(ma));
        }
//        for (String id : ids) {
//            userMapper.deleteByAttribute("itemId", id, ProductFormMap.class);
//        }
        return "success";
    }
}
